package com.llmcu.source.javase.thread.synchronizedtest;

import java.util.concurrent.TimeUnit;

/**
 *  静态方法上加synchronized锁的是类；
 *  普通方法上加synchronized锁的是类对象；
 *  所以两个方法不互斥
 *
 * @author liuling
 * @date 2024/11/25 11:58
 */
public class StaticSynchronized {
    public synchronized static void foo() throws InterruptedException {
        System.out.println(Thread.currentThread().getId()+"start foo...");
        TimeUnit.SECONDS.sleep(5);
        System.out.println(Thread.currentThread().getId()+"end foo...");
    }
    public synchronized void goo() throws InterruptedException {
        System.out.println(Thread.currentThread().getId()+"start goo...");
        TimeUnit.SECONDS.sleep(5);
        System.out.println(Thread.currentThread().getId()+"end goo...");
    }

    public static void main(String[] args) {
        StaticSynchronized staticSynchronized = new StaticSynchronized();
        new Thread(()->{
            try {
                foo();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{
            try {
                staticSynchronized.goo();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

    }
}
